﻿using HtmlAgilityPack;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Windows.System;
using Windows.UI;
using Windows.UI.Core;
using Windows8Accelerator.Core.Xaml.Caching;
using Windows8Accelerator.Core.Xaml.Controls;
using Windows8Accelerator.Core.Xaml.Converters;
using Windows8Accelerator.Core.Xaml.Html;

namespace Windows8Accelerator.Core.Xaml.Helpers
{
    public class HtmlHelper : HtmlHelperBase
    {     
        public override string ConvertToText(string html)
        {
            if (string.IsNullOrEmpty(html))
                return html;
            var result = Regex.Replace(html, "< .*?>", "");
            result = Regex.Replace(result, @"n+s+", "nn");
            result = HttpUtility.HtmlDecode(result);
            return result.Trim();
        }

        public override async Task PreLoadHtmlImagesAsync(string html)
        {
            html = WebUtility.HtmlDecode(html);

            var htmlDocument = new HtmlDocument();
            htmlDocument.LoadHtml(html);

            await VisitPreLoadHtmlImages(htmlDocument.DocumentNode);
        }

        private static async Task VisitPreLoadHtmlImages(HtmlNode htmlNode)
        {
            switch (htmlNode.NodeType)
            {
                case HtmlNodeType.Document:
                case HtmlNodeType.Element:
                    var tag = htmlNode.Name.ToLower();
                    if (tag.Equals("img") && htmlNode.Attributes.Contains("src"))
                    {
                        var imageSrc = htmlNode.Attributes["src"].Value;
                        try
                        {
                            await CacheManager.GetOrRetrieveUriAsync(imageSrc);
                        }
                        catch { }
                    }
                    else
                    {
                        foreach (var item in htmlNode.ChildNodes)
                        {
                            await VisitPreLoadHtmlImages(item);
                        }
                    }
                    break;
            }
        }

        private static void VisitGetHtmlImages(HtmlNode htmlNode, List<HtmlImage> result, Uri baseUri)
        {
            switch (htmlNode.NodeType)
            {
                case HtmlNodeType.Document:
                case HtmlNodeType.Element:
                    var tag = htmlNode.Name.ToLower();
                    if (tag.Equals("img") && htmlNode.Attributes.Contains("src"))
                    {
                        var htmlImage = new HtmlImage()
                        {
                            ImageSource = AdjustUrl(htmlNode.Attributes["src"].Value, baseUri)
                        };
                        if (htmlNode.Attributes.Contains("alt"))
                            htmlImage.AlternateText = htmlNode.Attributes["alt"].Value;
                        result.Add(htmlImage);
                    }
                    else
                    {
                        foreach (var item in htmlNode.ChildNodes)
                        {
                            VisitGetHtmlImages(item, result, baseUri);
                        }
                    }
                    break;
            }
        }
    }

 }
